package com.test;

import java.sql.*;

import com.fis.hthk.dblinkapi.dblinkImpl.dblinkImpl;


public class test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String db_connection = "WHTHK_wmwhse1"; // WHTMC_wmwhse1 for Macau and WFXLN_wmwhse1 for fix line
		String wmWhseID = "wmwhse1"; //the warehouse Id
		String provider = "192.168.205.153:11499"; //192.168.205.153: the IP of the server(DNS name can also be used). 11499: the port for connection. 11699 for Macau and 11999 for fix line

		String MoveKey = "1000000000"; // mandatory, can be generated by using dblinkImpl.getKey(provider, "WMWHSE1", "ADMIN", "movekey", "10", "1");
		String StorerKey = "HTHK"; // mandatory 
		String FromFacility = "U1"; // mandatory 
		String ToFacility = "PX"; // mandatory 
		String FromLoc = "PICKTO"; // mandatory 
		String ToLoc = "PX"; // mandatory 
		String Status = "0"; // mandatory
		String RMAKey = null; // can be null but not empty string e.g. ""
		String Reason = "1"; // mandatory
		String InternalReturnNo = null; // can be null but not empty string e.g. ""
		String MoveLineNumber ="00001"; // mandatory
		String MoveLineNumber2 ="00002"; 
		String Sku = "7APIPADS5"; // mandatory
		String Sku2 = "7APIPADS6";
		String Lot = "0000000017"; // mandatory
		String Qty = "1"; // mandatory
		String QtyDifference = null; // can be null but not empty string e.g. ""
		String DropID = " "; // mandatory
		String TOID = "0000000002"; // mandatory
		String FromID = "0000000002"; // mandatory
		String MSN = "1234"; // can be null but not empty string e.g. ""
		String PickDetailKey = "0000000002"; // can be null but not empty string e.g. ""


		try {
			//to get a new movekey for insert
			MoveKey = dblinkImpl.getKey(provider, "WMWHSE1", "ADMIN", "movekey", "10", "1");
		} catch (Exception e1) {
			e1.printStackTrace();
		}


		/**insert new record to moveheader*****/
		try {
			dblinkImpl.MoveHeaderInsert(db_connection, wmWhseID, provider, MoveKey, StorerKey, 
					FromFacility, ToFacility, FromLoc, ToLoc, Status, RMAKey, Reason, InternalReturnNo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		/******/

		// To check if the data is inserted into the movedetail
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			Connection conn = DriverManager.getConnection
					("jdbc:oracle:thin:@//192.168.205.153/WHTHK", "wmwhse1", "WMwhseSql1");

			Statement stmt = null;
			ResultSet rs = null;
			try {
				stmt = conn.createStatement();
				rs = stmt.executeQuery("select count(*) from movedetail where MoveKey = '" + MoveKey + "' and MoveLineNumber = '" + MoveLineNumber + "'");
				if (rs.next()){
					if(rs.getInt(1)==1){
						System.out.println ("Success"); 
					}
					else {
						System.out.println ("Failed"); 
					}
				}
			}
			finally {				
				rs.close();
				stmt.close(); 
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		/**insert new records to movedetail*****/
		try {
			dblinkImpl.MoveDetailInsert(db_connection, wmWhseID, provider, MoveKey, MoveLineNumber , StorerKey, Sku , Lot , FromLoc, ToLoc, Qty , QtyDifference, DropID, TOID, FromID, MSN, PickDetailKey);
			dblinkImpl.MoveDetailInsert(db_connection, wmWhseID, provider, MoveKey, MoveLineNumber2 , StorerKey, Sku2 , Lot , FromLoc, ToLoc, Qty , QtyDifference, DropID, TOID, FromID, MSN, PickDetailKey);
		} catch (Exception e) {
			e.printStackTrace();
		}
		/******/




		/**update movedetail e.g. edit MSN*****/
		try {
			dblinkImpl.MoveDetailUpdate(db_connection, wmWhseID, provider, MoveKey, MoveLineNumber, Sku, MSN);
		} catch (Exception e) {
			e.printStackTrace();
		} 
		/******/


		/**update moveHeader e.g. edit MSN*****/
		try {
			dblinkImpl.MoveHeaderUpdate(db_connection, wmWhseID, provider, MoveKey, Status);
		} catch (Exception e) {
			e.printStackTrace();
		} 
		/******/

		/**delete records from movedetail*****/
		try {
			dblinkImpl.MoveDetailDelete(db_connection, wmWhseID, provider, MoveKey, MoveLineNumber);
			dblinkImpl.MoveDetailDelete(db_connection, wmWhseID, provider, MoveKey, MoveLineNumber2);
		} catch (Exception e) {
			e.printStackTrace();
		}
		/******/


		/**delete a record from moveheader*****/
		try {
			dblinkImpl.MoveHeaderDelete(db_connection, wmWhseID, provider, MoveKey);
		} catch (Exception e) {
			e.printStackTrace();
		}
		/******/
	}
}
